package net.ys.controller;

import net.ys.service.ApiService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author: NMY
 * @Date: 2021/6/21
 * @Time: 14:42
 */
@Controller
public class ApiController {

    @Resource
    private ApiService apiService;

    @GetMapping("/ssq")
    public String ssq(@RequestParam(defaultValue = "0") int type, @RequestParam(defaultValue = "5") int account, Model model) {

        String genId = UUID.randomUUID().toString();

        List<List<Integer>> lists = apiService.genNum(type, account, genId);
        List<String> nums = new ArrayList<>();
        for (List<Integer> list : lists) {
            nums.add(list.toString().replaceAll("\\[|]", ""));
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        model.addAttribute("nums", nums);
        model.addAttribute("type", type);
        model.addAttribute("account", account);
        model.addAttribute("genId", genId);
        model.addAttribute("buy_date", sdf.format(new Date()));

        return "index";
    }

    @GetMapping("/buy")
    @ResponseBody
    public String buy(@RequestParam(value = "gen_id") String genId) throws Exception {

        boolean alreadyBuy = apiService.checkBuy();
        if (alreadyBuy) {
            System.out.println("has buy");
        }

        boolean result = apiService.buy(genId);
        if (result) {
            return "success";
        }
        return "error";
    }

    @GetMapping("/cashPrize")
    @ResponseBody
    public int cashPrize() throws Exception {
        return apiService.cashPrize();
    }

    @GetMapping("/genCpResultHis")
    @ResponseBody
    public String genCpResultHis() {
        apiService.genCpResultHis();
        return "success";
    }

    @GetMapping("/statisticsHq")
    @ResponseBody
    public String statisticsHq() {
        apiService.statisticsHq();
        return "success";
    }

    @GetMapping("/forecast")
    @ResponseBody
    public String forecast() {
        String result = apiService.forecast();
        return result;
    }
}
